package com.ng.desktop.hr.model;

//TODO agregar facebook, twitter, sitio web

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.IndexColumn;

@Entity
@Table(name = "HR_CUSTOMER")
public class Customer {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID", nullable = false)
	private Long id;

	@Column(name = "NAME", nullable = false, length = 60)
	private String name;

	@Column(name = "LAST_NAME", nullable = false, length = 60)
	private String lastName;

	@Column(name = "FULL_NAME", nullable = false, length = 120)
	private String fullName;

	@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
	@ForeignKey(name = "FK_CUSTOMER_EMAIL", inverseName = "FK_EMAIL")
	@JoinTable(name = "T_CUSTOMER_EMAIL", joinColumns = @JoinColumn(name = "ID_CUSTOMER"), inverseJoinColumns = @JoinColumn(name = "ID_EMAIL"))
	@IndexColumn(name = "IDX_CUSTOMER_EMAIL")
	private Set<Email> emails;

	@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
	@ForeignKey(name = "FK_CUSTOMER_PHONE", inverseName = "FK_PHONE")
	@JoinTable(name = "T_CUSTOMER_PHONE", joinColumns = @JoinColumn(name = "ID_CUSTOMER"), inverseJoinColumns = @JoinColumn(name = "ID_PHONE"))
	@IndexColumn(name = "IDX_CUSTOMER_PHONE")
	private Set<Phone> phones;

	@Column(name = "OBSERVATIONS", length = 1024)
	private String observations;

	@Column(name = "CREATION_DATE")
	private Date creationDate;

	@Column(name = "LAST_UPDATE")
	private Date lastUpdate;

	@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST})
	@ForeignKey(name = "FK_CUSTOMER_TAG", inverseName = "FK_TAG")
	@JoinTable(name = "T_CUSTOMER_TAG", joinColumns = @JoinColumn(name = "ID_CUSTOMER"), inverseJoinColumns = @JoinColumn(name = "ID_TAG"))
	@IndexColumn(name = "IDX_CUSTOMER_TAG")
	private List<Tag> tags;

	@Column(name = "PENDING")
	private Boolean pending;

	public Customer() {
		this.emails = new HashSet<Email>();
		this.phones = new HashSet<Phone>();
	}

	public Email getPrimaryEmail() {
		for (Email email : this.getEmails()) {
			if (email.getPrimaryUse()) {
				return email;
			}
		}
		return null;
	}

	public Phone getPrimaryPhone() {
		for (Phone phone : this.getPhones()) {
			if (phone.getPrimaryUse()) {
				return phone;
			}
		}
		return null;
	}

	public Long getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getLastName() {
		return lastName;
	}
	public Set<Email> getEmails() {
		return emails;
	}
	public Set<Phone> getPhones() {
		return phones;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	public Date getLastUpdate() {
		return lastUpdate;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public void setEmails(Set<Email> emails) {
		this.emails = emails;
	}
	public void setPhones(Set<Phone> phones) {
		this.phones = phones;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}
	public String getObservations() {
		return observations;
	}
	public List<Tag> getTags() {
		return tags;
	}
	public void setObservations(String observations) {
		this.observations = observations;
	}
	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}
	public Boolean getPending() {
		return pending == null ? Boolean.FALSE : pending;
	}
	public void setPending(Boolean pending) {
		this.pending = pending;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getFullName() {
		return fullName;
	}

	@Override
	public String toString() {
		return this.getFullName();
	}

	public void buildFullName() {
		this.fullName = this.name + " " + this.lastName;
	}

	public void buildFirstLastName(String fullName, String token) {
		String names[] = fullName.split(token);
		this.name = names[0];
		if(names.length <2){
			this.lastName = "S/D";
		}
		else{
			this.lastName = names[1];	
		}
		
	}
}
